package com.zk.controller;

import com.zk.pojo.PageResult;
import com.zk.pojo.R;
import com.zk.pojo.Student;
import com.zk.pojo.StudentVo;
import com.zk.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@RestController
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private StudentService studentService;

	//1. 查询所有学生
	@GetMapping("/list")
	public R findAll(){
		List<Student> studentList = studentService.findAll();
		return R.ok().data("students",studentList);
	}

	//2. 进行分页查询
	@GetMapping("/findByPage/{page}/{pageSize}")
	public R findByPage(@PathVariable int page,@PathVariable int pageSize){
		PageResult<Student> pr = studentService.findByPage(page,pageSize);
		return R.ok().data("pr",pr);
	}

	//3. 条件查询带分页
	@PostMapping("/search/{page}/{pageSize}")
	public R search(@PathVariable int page, @PathVariable int pageSize, @RequestBody StudentVo vo){
		PageResult<Student> pr = studentService.search(page,pageSize,vo);
		return R.ok().data("pr",pr);
	}

	//4. 添加学生
	@PostMapping("/add")
	public R add(@RequestBody Student student){
		studentService.add(student);
		return R.ok().message("添加学生成功!");
	}

	//5. 修改学生
	@PostMapping("/update")
	public R update(@RequestBody Student student){
		studentService.update(student);
		return R.ok().message("修改学生成功!");
	}

	//6. 删除学生
	@GetMapping("/delete/{sid}")
	public R delete(@PathVariable String sid){
		studentService.delete(sid);
		return R.ok().message("删除学生成功!");
	}
}
